<?php

namespace App\Listeners\CacheCounter;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalCounterConst;
use App\Events\CacheCounterEvent;
use App\Listeners\BaseListener;
use App\Models\Cache\Cache;

class CacheCounterListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param CacheCounterEvent $event
     * @return bool
     */
    public function handle(CacheCounterEvent $event)
    {
        $params = ArrayHelper::merge($event->params, [
            'trend' => $event->params['trend'] ?? GlobalCounterConst::INCREASE,
            'is_increment_update' => $event->params['is_increment_update'] ?? true,
        ]);
        $this->saveFileLog(__CLASS__, $params);

        Cache::getInstance($params['key_params'])
            ->incrFromArray($params['cache_key'], $params['values'], $params['trend'], $params['is_increment_update']);

        return true;
    }
}
